import java.io.*;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;

/**
 * 读取IOS的中文源文件，然后输出成IOS的中英文翻译文件
 * Created by heyiyong on 2016/5/13.
 */
public class PropertiesReader {
    public static final String xmlfileNameZh = "xml/latestFile.xml"; // 安卓中文源文件
    public static final String xmlfileNameEn = "xml/oldFile.xml"; // 安卓英文源文件
    public static final String fileName = "xml/zh.properties"; // IOS的源文件
    public static final String enFileName = "strings_en.properties"; // 替换后的输出文件（英文）
    public static final String zhFileName = "strings_zh.properties"; // 替换后的输出文件（中文）

    public static Map<String, String> xmlKeyValueZh = new TreeMap<>(); // 安卓的中文
    public static Map<String, String> xmlKeyValueEn = new TreeMap<>(); // 安卓的英文
    public static Map<String, String> proKeyValueZh = new TreeMap<>(); // IOS的中文
    public static Map<String, String> proKeyValueEnTemp = new TreeMap<>(); // IOS的英文（临时变量，方便于整理）
    public static Map<String, String> proKeyValueEn = new TreeMap<>(); // IOS的英文

    public static int enTimes = 0; // 英文替换个数
    public static int zhTimes = 0; // 中文替换个数

    public void launch() throws Exception {
        // 读取基础的properties文件，得到proKeyValueZh和proKeyValueEn
        readProperties();
        // 读取安卓的中文文件
        readXML_zh();
        // 读取安卓的英文文件
        readXML_en();
        // 根据相同的key，分别将中英文替换
        compareByKey();
        // 输出新的文件
        output(proKeyValueEn, enFileName);
        output(proKeyValueZh, zhFileName);
        // 输出统计信息
        System.out.println("enTimes = " + enTimes);
        System.out.println("zhTimes = " + zhTimes);
    }

    private void compareByKey() {
        Set<Map.Entry<String, String>> entrySet = proKeyValueEnTemp.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            String key = entry.getKey();
            // 替换中文
            String zhValue = xmlKeyValueZh.get(key);
            if (zhValue != null && !"".equals(zhValue)) { // 如果该key的val不为空，则替换
                proKeyValueZh.put(key, zhValue);
                zhTimes++;
            }
            // 替换英文
            String enValue = xmlKeyValueEn.get(key);
            if (enValue != null && !"".equals(enValue)) { // 如果该key的val不为空，则替换
                proKeyValueEn.put(key, enValue);
                enTimes++;
            }
        }
    }

    public static void output(Map<String, String> map, String fileName) throws Exception {
        URL resource = XmlSorter.class.getResource(PropertiesReader.fileName);
        File file = new File(resource.getFile());

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File(file.getParent(), fileName)));
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        String lastName = "";
        for (Map.Entry<String, String> entry : entrySet) {
            String key = entry.getKey();
            if (!key.startsWith(lastName)) {
                bufferedWriter.write("\r\n");
            }
            String value = filter(entry.getValue());
            bufferedWriter.write(key + "=" + value + "\r\n");
            lastName = key.substring(0, 5);
        }
        bufferedWriter.close();
    }

    /**
     * 过滤
     * @param value
     * @return
     */
    public static String filter(String value) {
        return value;
    }

    public void readXML_zh() throws Exception {
        Utils.iosRead(xmlfileNameZh, xmlKeyValueZh);
    }

    public void readXML_en() throws Exception {
        Utils.iosRead(xmlfileNameEn, xmlKeyValueEn);
    }

    public void readProperties() throws Exception {
        URL resource = XmlSorter.class.getResource(fileName);
        File file = new File(resource.getFile());
        Properties properties = new Properties();
        properties.load(new InputStreamReader(new FileInputStream(file), "GBK"));

        Set<Object> keySet = properties.keySet();

        for (Object key : keySet) {
            proKeyValueEnTemp.put(key.toString(), properties.getProperty(key.toString()));
            proKeyValueZh.put(key.toString(), properties.getProperty(key.toString()));
        }
    }

    public static void main(String[] args) throws Exception {
        PropertiesReader propertiesReader = new PropertiesReader();
        propertiesReader.launch();
    }

    /**
     * @deprecated
     */
    private void compareByValue() {
        Set<Map.Entry<String, String>> entrySet = proKeyValueEnTemp.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            String val = entry.getValue();
            // 遍历中文xml，得到相同value的那个key
            Set<Map.Entry<String, String>> entries = xmlKeyValueZh.entrySet();
            for (Map.Entry<String, String> e : entries) {
                if (e.getValue().equals(val)) {
                    String enVal = xmlKeyValueEn.get(e.getKey());
                    if (enVal != null && !"".equals(enVal)) {
                        proKeyValueEnTemp.put(entry.getKey(), enVal);
//                        times++;
                    }
                    break;
                }
            }
        }
    }
}
